<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>优惠券管理</el-breadcrumb-item>
      <el-breadcrumb-item>优惠券发放记录</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="时间">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.start_date"
              size="mini"
              style="width: 125px"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.end_date"
              size="mini"
              style="width: 125px"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="用户姓名" prop="user_id">
          <el-select
            v-model="queryInfo.query.user_id"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级栏目" prop="userName">
          <el-select
            v-model="queryInfo.query.channel_id"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option key="1" label="第三方渠道" value="1"> </el-option>
            <el-option key="2" label="小程序" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作名称" prop="keywords">
          <el-input
            v-model="queryInfo.query.keywords"
            placeholder="请输入手机号、优惠券名称或ID"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="getListData"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->

      <!-- 列表区域 -->
      <el-table :data="list" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="用户姓名" prop="operator_name"></el-table-column>
        <el-table-column
          label="二级栏目"
          prop="group_name"
          min-width="120px"
        ></el-table-column>
        <el-table-column
          label="操作名称"
          prop="operation_name"
          min-width="90px"
        ></el-table-column>
        <el-table-column
          label="操作详情"
          prop="operation_desc"
          min-width="150px"
          max-width="400px"
        ></el-table-column>
        <el-table-column label="时间" prop="create_time"></el-table-column>

      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          start_date: '',
          end_date: '',
          channel_id: '',
          user_id: '',
          keywords: ''
        },
        page: 1,
        size: 20
      },
      list: [],
      total: 0,
      userList: [],
      btnLoading: false
    }
  },
  created() {
    this.getQuerySelectData()
    this.getListData()
  },
  methods: {
    async getQuerySelectData() {
      const { data: res } = await this.$http.post('admin/Data/find', {
        code: 'user'
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.userList = res.data.user
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.queryInfo.query.start_date = ''
      this.queryInfo.query.end_date = ''
      this.getListData()
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        'admin/Logs/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.size = newSize
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
        this.getListData()
    },
    changeTab(id) {
      const path = '/coupon/queryList'
      this.$router.push({ path: path, query: { record_id: id } })
      this.$emit('updateActivePath', path)
      window.sessionStorage.setItem('activePath', path)
    },
    async downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/Coupon/CouponRecordExcel',
        data: this.queryInfo,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '优惠券发放记录.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          console.log(err)
          this.$message.error('下载失败')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.line {
  text-align: center;
}
</style>
